//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using System.Net;
using System.IO;

using Microsoft.Research.ScientificWorkflow;
using WorkflowData;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Write NetCDF")]
    [Description("Write out a NetCDF file based on the current Hypercube")]
    [WorkflowCategory("NetCdf")]
    public partial class NetCDF_Write : System.Workflow.ComponentModel.Activity
    {
        #region Property Definitions
        // Required input property
        public static DependencyProperty InputHyperCubeProperty =
            DependencyProperty.Register("InputHyperCube", typeof(HyperCube), typeof(NetCDF_Write));
        // Optional input properties
        public static DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(NetCDF_Write));
        // Output property
        public static DependencyProperty LinkProperty =
            DependencyProperty.Register("Link", typeof(string), typeof(NetCDF_Write));

        [RequiredInputParam]
        [Name("Input HyperCube", "Input")]
        [Description("Input data in HyperCube format. This is the data that will be written.")]
        public HyperCube InputHyperCube
        {
            get { return ((HyperCube)(base.GetValue(NetCDF_Write.InputHyperCubeProperty))); }
            set { base.SetValue(NetCDF_Write.InputHyperCubeProperty, value); }
        }

        [OptionalInputParam]
        [Name("FileName")]
        [Description("File location to write data to")]
        public string FileName
        {
            get { return ((string)(base.GetValue(NetCDF_Write.FileNameProperty))); }
            set { base.SetValue(NetCDF_Write.FileNameProperty, value); }
        }

        [OutputParam]
        [Name("Link")]
        [Description("Fully qualified link to file")]
        public string Link
        {
            get { return ((string)(base.GetValue(NetCDF_Write.LinkProperty))); }
            set { base.SetValue(NetCDF_Write.LinkProperty, value); }
        }
        #endregion

        public NetCDF_Write()
		{
			//InitializeComponent();
		}

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            string strFileOut;
            if (!string.IsNullOrEmpty(this.FileName))
                strFileOut = this.FileName;
            else
            {
                //create file location
                strFileOut = "http://skyline.cs.washington.edu:11223/datasets/ncom_icon_mbay_1.nc";
            }
            
            Console.WriteLine("Writing hypercube out to storage location");

            Link = strFileOut;

            return base.Execute(executionContext);
        }
	}
}
